package com.lxh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxh.config.utils.TreeUtils;
import com.lxh.entity.ChinaCity;
import com.lxh.entity.dto.ChinaCityDto;
import com.lxh.entity.vo.ChinaCityVo;
import com.lxh.mapper.ChinaCityMapper;
import com.lxh.service.ChinaCityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 中国省市区 服务实现类
 * </p>
 *
 * @author lxh
 * @since 2022-03-01
 */
@Service
public class ChinaCityServiceImpl extends ServiceImpl<ChinaCityMapper, ChinaCity> implements ChinaCityService {

    @Override
    public List<ChinaCityVo> getChinaTree(ChinaCityDto dto) {
        List<ChinaCityVo> treeList = this.getChinaList(dto).stream()
                .filter(item -> !item.getId().equals(item.getPid()))
                .map(item -> {
                    ChinaCityVo node = new ChinaCityVo();
                    node.setId(item.getId());
                    node.setPid(item.getPid());
                    node.setName(item.getName());
                    return node;
                }).collect(Collectors.toList());
        Collection<ChinaCityVo> collection = TreeUtils.toTree(treeList, "id", "pid", "children", ChinaCityVo.class);
        return (List<ChinaCityVo>) collection;
    }

    @Override
    public List<ChinaCity> getChinaList(ChinaCityDto dto) {
        return this.list(getCondition(dto));
    }

    @Override
    public IPage<ChinaCity> getPage(ChinaCityDto dto) {
        Page<ChinaCity> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        return this.page(page, getCondition(dto));
    }

    @Override
    public List<ChinaCity> getChinaTree1() {
        List<ChinaCity> parentList = new ArrayList<>();
        List<ChinaCity> list = this.list();
        //筛选出父级
        list.forEach(item -> {
           if (item.getPid() != null && item.getPid().equals(0) && item.getId() != 0) {
               parentList.add(item);
           }
        });
        //插入子级
        parentList.forEach(item -> {
           item.setChildren(getChildrenList(item.getId(), list));
        });
        return parentList;
    }

    public List<ChinaCity> getChildrenList(Integer pid, List<ChinaCity> list) {
        List<ChinaCity> childrenList = new ArrayList<>();
        list.forEach(item -> {
            if (pid.equals(item.getPid())) {
                childrenList.add(item);
            }
        });
        //递归
        childrenList.forEach(item -> {
           item.setChildren(getChildrenList(item.getId(), list));
        });
        return childrenList;
    }

    private LambdaQueryWrapper<ChinaCity> getCondition(ChinaCityDto dto) {
        LambdaQueryWrapper<ChinaCity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getId() != null, ChinaCity::getId, dto.getId())
                .eq(dto.getPid() != null, ChinaCity::getPid, dto.getPid())
                .like(dto.getName() != null, ChinaCity::getName, dto.getName());
        return queryWrapper;
    }
}
